<!--
 * @Author: Yang Zhang
 * @Date: 2021-12-04 13:08:00
 * @LastEditors: Yang Zhang
 * @LastEditTime: 2022-01-06 16:25:03
 * @Description: 
-->
<template>
  <div class="detailwrap">
    <div class="left">
      <div class="bigimg">
        <img :src="bigImg" />
      </div>
      <div class="smallimglst">
        <img :class="[nowIndex === index ? 'active' : '']" v-for="(item,index) in detailInfo.goodsGalleryList" :key="index" @click.stop="{ bigImg = item ; nowIndex = index }" :src="item"/>
      </div>
      <div class="intro" v-html="detailInfo.intro"></div>
    </div>
    <div class="right">
      <div class="productname">{{ detailInfo.goodsName }}</div>
      <div class="sellpoint">{{ detailInfo.sellingPoint }}</div>
      <div class="price">${{ detailInfo.price }}</div>
      <div class="info flex-sp-c">
        <div class="comm">Commission {{ detailInfo.commissionRate }}%</div>
        <div class="inventory">Inventory {{ detailInfo.quantity }}</div>
      </div>
      <div class="standards">
        <div class="standarditem" v-for="item in detailInfo.goodsParamsDTOList" :key="item.groupId">
          <div class="name">{{ item.groupName }}</div>
          <div class="list">
            <a-tag
              color="default"
              v-for="ele in item.goodsParamsItemDTOlist"
              :key="ele.paramId"
            >{{ ele.paramName }}</a-tag>
          </div>
        </div>
      </div>
      <div class="comfirmbtn" @click.stop="addPro(detailInfo.id)">Add to my product</div>
    </div>

    <!-- 添加成功的modal -->
    <div class="suc-modal" v-if="addsuccessful">
      <div class="icon">
        <img src="../../../assets/product/addsuceess.png" />
      </div>
      <div class="success">Successfully added to my product</div>
      <div class="desc">
        You can
        <span @click="gotoPage">go to release</span> products or continue to
        <span @click="gotoPage">view recommendations</span>
      </div>
    </div>
  </div>
  <div class="cardlst">
    <div
      class="kol-rec-list-item"
      v-for="item in recProlist"
      :key="item.brandid"
      @click.stop="gotoDetail(item)"
    >
      <div class="pro_img">
        <img :src="item.thumbnail" />
      </div>
      <div class="rec-desc overfl-oneline">{{ item.goodsName }}</div>
      <div class="rec-prize">${{ item.price }}</div>
      <div class="rec-com">Commission {{ item.commissionRate }}%</div>
      <div class="rec-addBtn" @click="addPro(item.id)">Add to my product</div>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { message } from 'ant-design-vue'
import { reactive, ref, toRefs, onBeforeMount, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getRecproducts } from '../../../api/home'
import { addProduct, getDetailByid } from '../../../api/product'
import { IProdetail } from '../../types'
interface DataProps { }
const router = useRouter()
const productId = ref('')
const gotoDetail = (item: { id: any }) => {
  initDetailinfo(item.id)
  getRecPros()
}

const addsuccessful = ref(false)
const gotoPage = () => {
  router.push('/product/index')
}

onMounted(() => {
  console.log(router.currentRoute.value.query.id)
  const id = router.currentRoute.value.query.id
  initDetailinfo(id)
  getRecPros()
})
const detailInfo = ref({} as IProdetail)
// 初始化产品详情
const initDetailinfo = async (id: any) => {
  const res = await getDetailByid(id)
  if (res) {
    detailInfo.value = res.result
    bigImg.value = detailInfo.value.goodsGalleryList[0]
  }
}

const bigImg = ref('')
const nowIndex = ref(0)

const recProlist = ref<any[]>([])             // 推荐商品列表
// 添加商品
const addPro = async (id: any) => {
  const res = await addProduct({ goodsId: id })
  if (res) {
    addsuccessful.value = !addsuccessful.value
    setTimeout(() => { addsuccessful.value = !addsuccessful.value }, 3000)
  }
}
// 获取推荐商品信息
const getRecPros = async () => {
  const res = await getRecproducts()
  if (res) {
    const arr = res.result.slice(0, 4)
    recProlist.value = arr
  }
}
</script>
<style lang='scss' scoped>
.detailwrap {
  width: 1600px;
  padding: 78px 75px 103px 103px;
  background-color: #fff;
  display: flex;
  position: relative;
  .left{
    width: 798px;
  }
  .bigimg {
    width: 798px;
    height: 798px;
    display: flex;
    justify-content: center;
    img {
      max-width: 798px;
    }
  }
  .suc-modal {
    width: 739px;
    height: 279px;
    background: #ffffff;
    box-shadow: 0px 0px 13px 0px rgba(219, 219, 231, 0.72);
    border-radius: 10px;
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    padding-top: 42px;
    .icon {
      width: 64px;
      height: 64px;
      margin: 0 auto;
      margin-bottom: 42px;
      img {
        width: 64px;
        height: 64px;
      }
    }
    .success {
      font-size: 24px;
      font-family: Galvji;
      font-weight: bold;
      color: #000000;
      line-height: 30px;
      text-align: center;
      margin-bottom: 32px;
    }
    .desc {
      font-size: 16px;
      font-family: Galvji;
      font-weight: 400;
      color: #000000;
      line-height: 30px;
      text-align: center;
      span {
        color: #e60044;
        cursor: pointer;
      }
    }
  }
  .smallimglst {
    margin-top: 18px;
    margin-bottom: 92px;
    img {
      height: 181px;
      margin-right: 47px;
      cursor: pointer;
    }
    .active {
      border: 2px solid #e60044;
    }
  }
  .intro {
    width: 100%;
    height: auto;
    img{
      max-width: 100%;
    }
  }
  .right {
    flex: 1;

    padding-left: 45px;
    .productname {
      font-size: 18px;
      font-family: Galvji;
      font-weight: bold;
      color: #000000;

      margin-bottom: 20px;
    }
    .sellpoint {
      width: 560px;
      font-size: 14px;
      font-family: Galvji;
      font-weight: normal;
      color: #000000;

      margin-bottom: 30px;
    }
    .price {
      font-size: 24px;
      font-family: Galvji;
      font-weight: bold;
      color: #000000;
      margin-bottom: 20px;
    }
    .info {
      margin-bottom: 45px;
      .comm {
        font-size: 24px;
        font-family: Galvji;
        font-weight: bold;
        color: #e60044;
      }
      .inventory {
        font-size: 18px;
        font-family: PingFang SC;
        font-weight: 300;
        color: #000000;
        line-height: 30px;
      }
    }

    .standards {
      height: 86px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-bottom: 94px;
      .standarditem {
        height: 32px;
        display: flex;
        align-items: center;

        .name {
          width: 64px;
          height: 32px;
          line-height: 32px;
          font-size: 18px;
          font-family: Galvji;
          font-weight: bold;
          color: #000000;
        }
        .list {
          flex: 1;
          height: 100%;
          display: flex;
          justify-content: space-between;
          padding-left: 14px;
          .ant-tag {
            height: 100%;
          }
        }
      }
    }

    .comfirmbtn {
      width: 450px;
      height: 48px;
      background: #000000;
      border-radius: 24px;
      font-size: 16px;
      font-family: Galvji;
      font-weight: bold;
      color: #ffffff;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 auto;
      cursor: pointer;
    }
  }
}

.cardlst {
  width: 1600px;
  display: flex;
  background-color: #fff;
  padding: 0 103px 89px;
  .kol-rec-list-item {
    width: 339px;
    background: #ffffff;
    margin-bottom: 20px;
    margin-right: 14px;
    padding: 16px;
    cursor: pointer;
    .pro_img {
      width: 312px;
      height: 312px;
      background-color: red;
      margin-bottom: 13px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .rec-desc {
      height: 40px;
      font-size: 14px;
      font-family: Galvji;
      font-weight: 400;
      color: #000000;
      margin-bottom: 12px;
    }
    .rec-prize {
      font-size: 16px;
      font-family: Galvji;
      font-weight: bold;
      color: #000000;
      margin-bottom: 10px;
    }
    .rec-com {
      font-size: 16px;
      font-family: Galvji;
      font-weight: bold;
      color: #e60044;
      margin-bottom: 5px;
    }
    .rec-addBtn {
      width: 312px;
      height: 53px;
      border: 1px solid #e0e0e0;
      font-size: 15px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #000000;
      display: inline-flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }
    .rec-addBtn:hover {
      background-color: #000;
      color: #fff;
    }
  }
  .kol-rec-list-item:hover {
    box-shadow: 0px 0px 13px 0px rgba(219, 219, 231, 0.72);
  }
}
</style>